package parser.part.n1;

interface Rule {

    public Object exequteRule();

    void setArgs(int position, Object value);
}

abstract class Ruler implements Rule {

    Object[] args;

    public Ruler(int arguments_count) {
        this.args = new Object[arguments_count];

    }

    public Ruler() {
        this.args = new Object[16];

    }

    @Override
    public void setArgs(int position, Object value) {

        if (args.length > position) {

            System.out.println("in position " + position + " of " + args.length + " value: " + value);
            args[position] = value;
        } else {
            System.out.println(args.length + " < " + position);
        }

    }
}

class Find extends Ruler {

    Find(int i) {
        super(i);
    }

    @Override
    public Object exequteRule() {
        String s = (String) args[0];
        String f = (String) args[1];
        int i = 0;
        int count = 0;
        while (s.indexOf(f, i) != -1) {
            i = s.indexOf(f, i) + 1;
            count++;
        }
        return count;
    }
}

class Concat extends Ruler {

    Concat(int i) {
        super(i);
    }

    @Override
    public Object exequteRule() {
        String s = (String) args[0];
        String f = (String) args[1];
        return s.concat(f);
    }
}

class Check extends Ruler {

    @Override
    public Object exequteRule() {

        String s = (String) args[0];
        String f = (String) args[1];


        if (s != null || f != null) {
            return true;
        } else {
            return null;
        }
    }

    public Check(int arguments_count) {
        super(arguments_count);
    }
}

class Summ extends Ruler {

    public Summ(int arguments_count) {
        super(arguments_count);
    }

    @Override
    public Object exequteRule() {

        System.out.println("***********args***********");
        System.out.println("length :" + args.length);
        System.out.println("args[0]:" + args[0]);
        System.out.println("args[1]:" + args[1]);


        if (args[0] != null && args[1] != null) {
            int ret = (Integer) args[0] + (Integer) args[1];
            System.out.println("returned: " + ret);
            return ret;
        }


        return -1;

    }
}
